<?php
/**
 * User: Administrator
 * Note: 文章管理
 */
namespace Admin\Controller;

class ArticleController extends BaseController
{
    public function index()
    {
        if (IS_POST) {
            $title      = I('title', '', 'trim');
            $date       = I('date', '', 'trim');
            $page_count = I('limit', '', 'intval');
            $page       = I('page', '', 'intval');
            $map        = array();
            if ($title) {
                $map['title'] = array('like','%'.$title.'%');
            }
            if ($date) {
                list($start_time, $end_time) = explode(' - ', $date);
                $start_time = $start_time?strtotime(trim($start_time)):0;
                $end_time = $end_time?strtotime(trim($end_time))+24*60*60:0;
                if ($start_time && $end_time) {
                    $map['add_time'] = array(array('egt',$start_time),array('elt',$end_time));
                }
            }
            $total = M('article')->where($map)->count();
            $limit = ($page - 1)*$page_count.','.$page_count;
            $_list = M('article')->alias('a')
                ->join('LEFT JOIN tp_article_type aa on aa.id=a.type_id')
                ->field(array('a.id'=>'id','a.*','aa.name'))
                ->where($map)
                ->limit($limit)
                ->order('a.id desc')
                ->select();
            foreach ($_list as &$item) {
                $item['content']  = htmlspecialchars(mb_substr($item['content'], 0, 100));
                $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            }
            layuiReturn(0, '成功', $_list, $total);
        } else {
            $this->assign('meta_title', '文章管理');
            $this->display();
        }
    }

    // 启禁用删除
    public function changeStatus()
    {
        $method = I('method', '', 'trim');
        $ids    = I('ids');
        $map['id'] = array('IN',$ids);
        switch ($method) {
            case 'enable':
                $res = M('article')->where($map)->save(array('status'=>'1'));
                if ($res === false) {
                    appReturn(0, '操作失败');
                } else {
                    appReturn(1, '操作成功');
                }
                break;
            case 'disable':
                $res = M('article')->where($map)->save(array('status'=>'0'));
                if ($res === false) {
                    appReturn(0, '操作失败');
                } else {
                    appReturn(1, '操作成功');
                }
                break;
            case 'del':
                $res = M('article')->where($map)->delete();
                if ($res === false) {
                    appReturn(0, '操作失败');
                } else {
                    appReturn(1, '操作成功');
                }
                break;
            default:
                appReturn(0, '参数错误');
        }
    }

    public function add()
    {
        if (IS_POST) {
            $title    = I('title', '', 'trim');
            $content  = I('content', '', 'trim');
            $picture  = I('picture', '', 'trim');
            $status   = I('status', 0, 'intval');
            $type     = I('type', 0, 'intval');
            $add_time = I('add_time', '', 'trim');
            if (!$title || !$content) {
                appReturn(0, '请填写标题和内容');
            }
            if(!$add_time){
                $add_time = time();
            }else{
                $add_time = strtotime($add_time);
            }
            $data = array(
                'title'     =>  $title,
                'picture'   =>  $picture,
                'content'   =>  $content,
                'add_time'  =>  $add_time,
                'type_id'   =>  $type,
                'status'    =>  $status
            );
            $res = M('article')->add($data);
            if (!$res) {
                appReturn(0, '添加失败');
            } else {
                appReturn(1, '添加成功', '', U('Article/index'));
            }
        } else {
            $type = M('article_type')->select();
            $this->assign('types', $type);
            $this->assign('meta_title', '新增文章');
            $this->display();
        }
    }

    public function edit()
    {
        if (IS_POST) {
            $id       = I('id', 0, 'intval');
            $title    = I('title', '', 'trim');
            $picture  = I('picture', '', 'trim');
            $content  = I('content', '', 'trim');
            $status   = I('status', 0, 'intval');
            $type     = I('type', 0, 'intval');
            $add_time = I('add_time', '', 'trim');
            if (!$id) {
                appReturn(0, '参数错误');
            }
            if (!$title || !$content) {
                appReturn(0, '请填写标题和内容');
            }
            if(!$add_time){
                $add_time = time();
            }else{
                $add_time = strtotime($add_time);
            }
            $data = array(
                'title'     =>  $title,
                'picture'   =>  $picture,
                'content'   =>  $content,
                'status'    =>  $status,
                'type_id'   =>  $type,
                'add_time'  =>  $add_time
            );
            $res = M('article')->where(array('id'=>$id))->save($data);
            if (!$res) {
                appReturn(0, '编辑失败');
            } else {
                appReturn(1, '编辑成功', '', U('Article/index'));
            }
        } else {
            $id = I('id', 0, 'intval');
            if (!$id) {
                $this->error('参数错误', U('Article/index'));
            }
            $info = M('article')->where(array('id'=>$id))->find();
            if (!$info) {
                $this->error('参数错误', U('Article/index'));
            }
            $info['add_time'] = date('Y-m-d H:i:s', $info['add_time']);
            $type = M('article_type')->select();
            $this->assign('types', $type);
            $this->assign('info', $info);
            $this->assign('meta_title', '新增文章');
            $this->display();
        }
    }

    public function uploadImg_del()
    {
        $return = array(
            'code'  =>  233,//0成功
            'msg'   =>  '',
            'data'  =>  array('src'=>'','title'=>'')
        );
        $upload = new \Think\Upload(array('exts'=>'jpg,gif,png,jpeg'));

        $upload->rootPath = APP_PATH.'../Public/Admin/images/';
        $info = $upload->upload($_FILES);
        if (!$info) {
            $return['msg'] = $upload->getError();
            echo json_encode($return);
        } else {
            $return['code'] = 0;
            $return['msg']  = '上传成功';
            $return['data']['src'] = 'http://'.$_SERVER['HTTP_HOST'].'/Public/Admin/images/'.$info['file']['savepath'].$info['file']['savename'];
            echo json_encode($return);
        }
    }

    //上传币种logo
    public function uploadImg()
    {
        $return = array(
            'code'  =>  233,//0成功
            'msg'   =>  '',
            'data'  =>  array('src'=>'','title'=>'')
        );
        $upload = new \Think\Upload(array('exts'=>'jpg,gif,png,jpeg'));
        $info   = $upload->upload($_FILES);
        if (!$info) {
            $return['msg'] = $upload->getError();
            echo json_encode($return);
        } else {
            $return['code'] = 0;
            $return['msg']  = '上传成功';
            $return['data']['src'] = '/Uploads/'.$info['file']['savepath'].$info['file']['savename'];
            echo json_encode($return);
        }
    }

    public function del_type()
    {
        $method = I('method', '', 'trim');
        $ids    = I('ids');
        $map['id'] = array('IN',$ids);
        switch ($method) {
            case 'del':
                $res = M('article_type')->where($map)->delete();
                if ($res === false) {
                    appReturn(0, '操作失败');
                } else {
                    appReturn(1, '操作成功');
                }
                break;
            default:
                appReturn(0, '参数错误');
        }
    }
    public function edit_type()
    {
        if (IS_POST) {
            $name = I('name', '', 'trim');
            $id   = I('id', '', 'trim');
            $page_count = I('limit', '', 'intval');
            $page = I('page', '', 'intval');

            $total = M('article_type')->count();
            $limit = ($page - 1)*$page_count.','.$page_count;

            $address = M('article_type')->alias('t')
                ->limit($limit)
                ->select();

            //dump($address);
            layuiReturn(0, '成功', $address, $total);
        }

        $info = M('article_type')->select();
        if (!$info) {
            $this->error('参数错误', U('User/index'));
        }

        $this->assign('meta_title', '编辑文章类型');
        $this->assign('user', $info);
        $this->display();
    }

    public function do_edit_type()
    {
        if (IS_POST) {
            $id   = I('id', '', 'trim');
            $name = I('name', '', 'trim');

            if (M('article_type')->where(array('id'=>$id))->save(array('name'=>$name))) {
                appReturn(1, '操作成功');
            } else {
                appReturn(0, '操作失败');
            }
        }
    }

    public function add_type()
    {
        if (IS_POST) {
            $name = I('name', '', 'trim');
            if (!$name) {
                appReturn(0, '请填写类型名称');
            }

            $data = array(
                'name'  =>  $name,
            );
            $res = M('article_type')->add($data);
            if (!$res) {
                appReturn(0, '添加失败');
            } else {
                appReturn(1, '添加成功', '', U('Article/index'));
            }
        } else {
            $this->assign('meta_title', '添加文章类型');
            $this->display();
        }
    }
}
